package marchsoft.modules.selfFish.pisciculture.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import marchsoft.exception.BadRequestException;
import marchsoft.modules.selfFish.pisciculture.entity.dto.KnowledgeDto;
import marchsoft.modules.selfFish.pisciculture.service.KnowledgeService;
import marchsoft.response.Result;
import marchsoft.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**养鱼知识库
 * @title FishController
 * @description 养鱼知识库
 * @author Lenovo
 * @version 1.0
 * @create 2024-07-15 16:53
 */
@Slf4j
@RestController
@RequestMapping("/api/knowledge")
@Api(tags = "养鱼管理：养鱼知识库")
public class KnowledgeController {

    @Autowired
    private KnowledgeService knowledgeService;

    private static final String ENTITY_NAME = "knowledge";

    @GetMapping
    @ApiOperation("获取养鱼知识库列表")
    @PreAuthorize("@smpe.check('knowledge:list')")
    public Result<Object> query(String content,
                                @RequestParam(value ="pageNum",defaultValue = "1")Long pageNum,
                                @RequestParam(value ="pageSize",defaultValue = "10") Long pageSize ){
        log.info(StrUtil.format("【获取养鱼知识库列表 /api/knowledge】操作人id：{}，养鱼知识库查询条件 knowledgeName：{}，分页pageNum：{}", SecurityUtils.getCurrentUserId(),content, pageNum));
        return knowledgeService.getKnowledgeList(content,pageNum,pageSize);
    }

    @PostMapping
    @ApiOperation("新增养鱼知识库")
    @PreAuthorize("@smpe.check('knowledge:add')")
    public Result<Object> create(@RequestBody KnowledgeDto knowledgeDto){
        log.info(StrUtil.format("【新增养鱼知识库 /api/knowledge】操作人id：{}，新增养鱼知识库实体：{}", SecurityUtils.getCurrentUserId(), knowledgeDto));
        if (ObjectUtil.isNotNull(knowledgeDto.getId())) {
            log.error(StrUtil.format("【新增养鱼知识库失败】操作人id：{}，新增养鱼知识库实体默认id应该为空，dept：{}", SecurityUtils.getCurrentUserId(), knowledgeDto));
            throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID");
        }
        return knowledgeService.create(knowledgeDto);
    }

    @DeleteMapping
    @ApiOperation("删除养鱼知识库")
    @PreAuthorize("@smpe.check('knowledge:del')")
    public Result<Object> delete(@RequestBody Set<Long> ids){
        log.info(StrUtil.format("【删除养鱼知识库 /api/knowledge】操作人id：{}，被删除养鱼知识库id集：{}", SecurityUtils.getCurrentUserId(), ids.toString()));
        return knowledgeService.delete(ids);
    }

    @PutMapping
    @ApiOperation("修改养鱼知识库")
    @PreAuthorize("@smpe.check('knowledge:edit')")
    public Result<Object> update(@RequestBody KnowledgeDto knowledgeDto){
        log.info(StrUtil.format("【修改养鱼知识库 /api/knowledge】操作人id：{}，被修改养鱼知识库id：{}", SecurityUtils.getCurrentUserId(), knowledgeDto.getId()));
        return knowledgeService.update(knowledgeDto);
    }

    @ApiOperation("导出养鱼知识库数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@smpe.check('knowledge:list')")
    public void download(HttpServletResponse response, String content) throws IOException {
        log.info(StrUtil.format("【查询养鱼知识库 /api/knowledge】操作人id：{}，养鱼知识库查询条件 fishName：{}", SecurityUtils.getCurrentUserId(), content));
        knowledgeService.download(content,response);
    }
}
